﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.Models;
using WineStore.DAL;
using System.Data;
using System.Web.Security;
using PagedList.Mvc;
using PagedList;

namespace WineStore.Controllers
{
    public class CustomerController : Controller
    {
        // GET: /Customer/ShowCustomer/
        public ViewResult ShowCustomer(int? page)
        {

            using (var db = new WineDbContext())
            {
                var customers = db.Customers
                    .OrderByDescending(c => c.CustomerId);

                int pageSize = 30;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;

                return View(customers.ToPagedList(pageNumber, pageSize));
            }
        }

        // GET: /Customer/DeleteCustomer/
        public ActionResult DeleteCustomer(int id)
        {
            Customer customer = null;
            using (var db = new WineDbContext())
            {
                customer = db.Customers.Find(id);
                if (customer == null)
                {
                    return HttpNotFound();
                }
            }
            return View(customer);
        }

        // POST: /Customer/DeleteCustomer/
        [HttpPost, ActionName("DeleteCustomer")]
        public ActionResult DeleteConfirmed(int id)
        {
            Customer customer = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    customer = db.Customers.Find(id);
                    db.Entry(customer).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowCustomer", "Customer");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(customer);
        }
    }
}
